# -*- coding: utf-8 -*-
# coding: UTF-8
import os
import logging
from scrapy.spider import CrawlSpider
from scrapy.selector import Selector
from scrapy.http import Request
from uoco import settings

from ..items import UocoItem


class uocoSpider(CrawlSpider):
    name = 'uocoSpider'
    host = 'https://uoco.co/forum.php?%s'
    start_urls = ['mod=forumdisplay&fid=2']
    logging.getLogger("requests").setLevel(logging.WARNING)  # 将requests的日志级别设成WARNING
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
        datefmt='%a, %d %b %Y %H:%M:%S',
        filename='cataline.log',
        filemode='w')
    custom_settings = {
        'ITEM_PIPELINES': {
            'uoco.pipelines.UocoPipeline': 1,
        }
    }

    # test = True
    def start_requests(self):
        for db_type in self.start_urls:
            yield Request(url=self.host % db_type,
                          callback=self.parse_db_key)

    def parse_db_key(self, response):
        sel = Selector(response)
        logging.debug('request url:------>' + response.url)
        lists = sel.xpath('//*[@id="moderate"]/li')

        for li in lists:
            host = 'https://uoco.co/'
            link = li.xpath('div/a/@href').extract()
            yield Request(url=host + link[0],
                          callback=self.parse_ph_info)

    def parse_ph_info(self, response):
        item = UocoItem()
        sel = Selector(response)

        # name = item['name'] = sel.xpath('//a[@class="deanlzname"]/text()').extract()
        # name = item['name'] = sel.xpath('//div[@class="deanvipinfos"]/h5/text()').extract()
        # path = settings.IMAGES_STORE + name[0] + '/'
        # if os.path.exists(path) == False:
        #     os.mkdir(path)

        imgs = sel.xpath('//img[@class="zoom"]/@zoomfile').extract()
        host = 'https://uoco.co/'
        for img in imgs:
            yield host + img
        item['img_urls'] = imgs
        
        yield item
